/****************************************************************************
 * Copyright (C) 2012
 * by dj_skual
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 *
 * DevicesSwap.h
 *
 * for SaveGame Manager GX 2012
 ***************************************************************************/
#ifndef _DEVICESSWAP_H
#define _DEVICESSWAP_H

#include "../Prompts/ThrobberWindow.h"
#include "../Controls/CThread.h"
#include "../stdafx.h"

class DevicesSwap : public CThread, public sigslot::has_slots<>
{
	public:
		static DevicesSwap * Instance() { if(!instance) instance = new DevicesSwap(); return instance; }
		static void DestroyInstance() { if(instance) delete instance; instance = NULL; }
		
		void SwapUSBPorts() { SwapPorts = true; }
		bool SwappingUSBPorts() { return SwapPorts; }
		void SetExplorerReload() { ExplorerReload = true; }
		bool IsWaiting() { return WaitingWindowOn; }
		
		sigslot::signal2<int, bool> Swapped;
		
		enum
		{
			SD = 0,
			USB0,
			USB1,
			GCA,
			GCB,
		};
		
	private:
		DevicesSwap();
		virtual ~DevicesSwap();
		
		static DevicesSwap * instance;
		
		void StateInit();
		void GetConfigDevice();
		
		void CheckState_SD();
		void CheckState_USB0();
		void CheckState_USB1();
		void CheckState_GCA();
		void CheckState_GCB();
		
		void ShowWaitInsert();
		void CloseWaitInsert();
		
		void SwapUSB();
		
		void executeThread(void);
		
		bool ExitRequested;
		bool SwapPorts;
		bool ExplorerReload;
		bool WaitingWindowOn;
		int usedIOS;
		int ConfigDevice;
		
		typedef struct _StateStruct
		{
			bool sdInserted;
			bool usb0Inserted;
			bool usb1Inserted;
			bool gcaInserted;
			bool gcbInserted;
		} StateStruct;
		
		StateStruct * state;
		StateStruct * newState;
		
		Timer * timer;
		
		ThrobberWindow * Prompt;
};

#endif
